package com.xd.study.activity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

import com.xd.study.R;

/**
 * Created by DGF on 2015/4/23.
 */
public class WebViewActivity extends Activity {

    private WebView webView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.webview);

//        //启动系统自带的浏览器打开链接
//        Uri uri = Uri.parse("http://www.imooc.com");
//        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
//        startActivity(intent);

        webView = (WebView)this.findViewById(R.id.webview);
        //加载web资源
        webView.loadUrl("http://www.baidu.com");
        //加载本地资源 -- 无JavaScript的网页直接load即可
//        webView.loadUrl("file:///android_asset/withoutJS.html");
        //加载本地资源 -- 带JavaScript的网页需要启用JavaScript
//        webView.loadUrl("file:///android_asset/withJS.html");

        //WebView默认使用系统默认或第三方浏览器打开，此处需要自定义WebViewClient以覆盖默认行为
        //WebViewClient帮助WebView处理一些页面控制和请求通知
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
//                return super.shouldOverrideUrlLoading(view, url);

                //返回true时，在WebView中打开
                view.loadUrl(url);
                return true;
            }
        });

        WebSettings webSettings = webView.getSettings();
        //启用JavaScript
        webSettings.setJavaScriptEnabled(true);
        //速度优化：加载页面优先使用缓存加载
        webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);

        //显示加载进度
        webView.setWebChromeClient(new WebChromeClient(){

            ProgressDialog dialog = null;

            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                //super.onProgressChanged(view, newProgress);

                //newProgress在1-100之间
                if (newProgress == 100){
                    //网页加载完成，关闭进度对话框
                    closeDialog();
                }else{
                    //网页正在加载，显示进度对话框
                    openDialog(newProgress);
                }
            }

            private void openDialog(int progress) {
                if (dialog == null){
                    dialog = new ProgressDialog(WebViewActivity.this);
                    dialog.setTitle("正在加载，请稍候");
                    dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                    dialog.setProgress(progress);
                    dialog.show();
                }else{
                    dialog.setProgress(progress);
                }
            }

            private void closeDialog() {
                if (dialog != null && dialog.isShowing()){
                    dialog.dismiss();
                    dialog=null;
                }
            }
        });
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        //改写物理按键，实现网页后退的功能
        if (keyCode == KeyEvent.KEYCODE_BACK){
            Toast.makeText(this, webView.getUrl(), Toast.LENGTH_SHORT).show();//打印网址
            if (webView.canGoBack()){
                webView.goBack();//后退
                return true;
            }else{
                System.exit(0);//退出程序
            }
        }

        return super.onKeyDown(keyCode, event);
    }
}
